/**
 * 新建会议加载界面
 * 获取数据
 */
$(function() {
	$("#meeting_time").datepicker({
		changeMonth: true,
		changeYear: true,
		numberOfMonths: 3,
		dateFormat: "yy-mm-dd"
	});
	getDeptinfo();
});
function new_com_Cmeeting(){
	console.log("例会");
	$("#meeting_type_show").val("例会");
}
function new_pro_Cmeeting(){
	console.log("立项会议");
	$('#meeting_type_show').val("立项会");
}
function new_dis_Cmeeting(){
	console.log("讨论会");
	$("#meeting_type_show").val("讨论会");
}
function new_dept_Cmeeting(){
	console.log("部门会议");
	$("#meeting_type_show").val("部门会议");
}
/**
 * 点击添加会议任务栏
 */
var task_div_id = 0;
var task_time_id=0;
var deptInfo = [];

function addMeetingTask() {
	var addHtml = "<div calss='row' id='task_div_id_" + task_div_id + "' style='margin-top:20px;'>" +
		"<div class='col-sm-2'>任务名称:<input type='text' class='form-control' name='commonMeetingTaskName'></div>" +
		"<div class='col-sm-2'>开始时间：<input type='text' class='form-control' id='task_starttime_id_"+task_div_id +"' name='commonMeetingTaskStartTime'></div>" +
		"<div class='col-sm-2'>结束时间：<input type='text' class='form-control' id='task_endtime_id_"+task_div_id +"' name='commonMeetingTaskEndTime'></div>" +
		"<div class='col-sm-2'>任务KPI分值：<input type='text' class='form-control' name='commonMeetingTaskPoint'></div>" +
		"<div class='col-sm-2'>选择部门：<select class='form-control' id='task_dept_id_"+task_div_id+"' name='commonMeetingTaskDept'></select></div>" +
		"<div class='col-sm-2'>任务内容：<input type='text' class='form-control'name='commonMeetingTaskComment' ></div>" +
		"</div>";
	$("#new_meeting_add_task").append(addHtml);
	$("#task_starttime_id_" + task_div_id).datepicker({
		changeMonth: true,
		changeYear: true,
		 numberOfMonths: 3,
		dateFormat: "yy-mm-dd",
		onClose:function(selectedDate){
			var end_id = $(this)[0].id.replace("starttime","endtime");
			$("#"+end_id).datepicker( "option", "minDate", selectedDate );
		}
	});
	$("#task_endtime_id_" + task_div_id).datepicker({
		changeMonth: true,
		changeYear: true,
		numberOfMonths: 3,
		dateFormat: "yy-mm-dd",
		onClose:function(selectedDate){
			var start_id = $(this)[0].id.replace("endtime","starttime");
			$("#"+start_id).datepicker( "option", "maxDate", selectedDate );
		}
	});
	//绑定部门
	
	var addHtml = "";
	for(var i=0;i<deptInfo.length;i++){
		addHtml = addHtml + "<option value="+ deptInfo[i][1]+">"+ deptInfo[i][0]+"</option>";
	}
	$("#task_dept_id_" + task_div_id).append(addHtml);
	task_div_id++;
}
/**
 * 点击删除任务栏
 */
function delMeetingTask() {
	console.log("#task_div_id_" + (task_div_id - 1));
	$("#task_div_id_" + (task_div_id - 1)).remove();
	task_div_id--;
}
/**
 * 获取部门列表
 */

function getDeptinfo() {
	$.ajax({
		type: "post",
		url: "deptInfo",
		async: true,
		cache: false,
		datatype: "json",
		success: function(data) {
			deptInfo = data;
		},
		complete: function() {
			var addHtml = "";
			for(var i = 0; i < deptInfo.length; i++) {
				addHtml = addHtml + "<a  data-toggle='modal' class='list-group-item' id=" + deptInfo[i][1] + " href='#addParticipantByDept' onclick='showDeptMemberInfo(this)'>" + deptInfo[i][0] + "</a>";
			}
			$("#modal_dept_info").append(addHtml);
		}
	});
}
/**
 * 获取部门的信息
 * @param {Object} deptId
 */
var global_emp_id = [];
function showDeptMemberInfo(deptId) {
	var id = deptId.id;
	var empInfo = [];
	$.ajax({
		type: "post",
		url: "empInfo",
		cache: false,
		async: true,
		datatype: "json",
		data: {
			deptid: id
		},
		success: function(data) {
			empInfo = data;
		},
		complete: function() {
			$("#dept_member_info").empty();
			var addHtml = "";
			for(var i = 0; i < empInfo.length; i++) {
				addHtml = addHtml + "<div class='col-sm-2'><input type='checkbox' value='" + empInfo[i].id + "'/><span id='check_span_" + empInfo[i].id + "'>" + empInfo[i].name + "</span></div>";
			}
			$("#dept_member_info").append(addHtml);
		}
	});
}
/**
 * 设置全局数组存储选择的成员
 */
var emp_k = 0

function setGlobalEmpInfo() {
	var checked_input = $("#dept_member_info input:checked");
	for(var i = 0; i < checked_input.length; i++) {
		var emp = {};
		emp.id = checked_input[i].value;
		emp.name = $("#check_span_" + checked_input[i].value).text();
		global_emp_id[emp_k] = emp;
		emp_k++;
	}
}
/*
 * 设置选择框
 */
function setEmpInfoForSubmit() {
	$("#member_chosed").empty();
	var arr = [];
	var list = global_emp_id;
	
	for(var i = 0; i < list.length; i++) {
		if(i == 0) arr.push(list[i]);
		b = false;
		if(arr.length > 0 && i > 0) {
			for(var j = 0; j < arr.length; j++) {
				if(arr[j].name == list[i].name) {
					b = true;
					//break;
				}
			}
			if(!b) {
				arr.push(list[i]);
			}
		}
	}
	var addHtml = "";
	for(var x = 0; x < arr.length; x++) {
		//console.log(arr[x]);
		addHtml = addHtml + "<div class='col-sm-1'><input type='checkbox' checked name='meetingEmps' value='" + arr[x].id + "'/><span id='check_span_" + arr[x].id + "'>" + arr[x].name + "</span></div>";
			}
	$("#member_chosed").append(addHtml);
}
/**
 * 表单提交之前验证数据是否合理
 */
var modalCheck = 1;
function checkFormBeforeSubmit(){
	console.log("表单验证");
	var mname = $('input[name="mname"]').val();
	if(mname == ""){
		$('#name_check').text("请输入会议名称");
		$('input[name="mname"]').focus();
		return false;
	}
	
	var mtime = $('input[name="meetingDate"]').val();
	if(mtime == ""){
		$('#time_check').text("请选择会议时间");
		$('input[name="meetingDate"]').focus();
		return false;
	}
	
	var mcontent = $('input[name="meetingContent"]').val();
	if(mcontent == ""){
		$('#content_check').text("请输入会议内容");
		$('input[name="meetingContent"]').focus();
		return false;
	}
	
	var mparticipants = $('input:checked[name="meetingEmps"]');
	if(mparticipants.length ==0){
		$('#participants_check').text("请选择参会人员");
		return false;
	}
	
	if($("#new_meeting_add_task").children().length == 1 && modalCheck == 1){
		modalCheck = 0;
		$('#tasksFormeeting').modal('show');
	}
	var taskname = $("#new_meeting_add_task input");
	console.log(taskname.length);
	
	
	if(taskname.length > 0){
		for(var i=0;i<taskname.length;i++){
			if(taskname[i].value == ""){
				$("#tasks_inform").text("任务信息输入不完整，请重新确认！")
				return false;
			}
		}
	}
	var taskPoint = $("input[name='commonMeetingTaskPoint']");

var isSubmit = true;
	taskPoint.each(function(){
		if(!isNaN($(this).val())){
			if($(this).val()>20){
				console.log($(this).val());
				$("#tasks_inform").text("任务分数大于上限，请重新输入分数！");
		}
		}else{
			$("#tasks_inform").text("请输入数字格式的分数！");
		}
		isSubmit = false;
	});
	
	return isSubmit;
}
/**
 * 提交会议
 */
function sumComMeetingForm(){
	commMeetingForm.submit();
}
function returnBackToAddTask(){
	modalCheck = 1;
}
